﻿<%@ WebHandler Language="C#" Class="CityHandler" %>

using System;
using System.Web;
using System.Collections;
using System.Data;
using StarTech.DBUtility;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Web.DynamicData;


public class CityHandler : IHttpHandler
{
    AdoHelper ado = AdoHelper.CreateHelper(StarTech.Util.AppConfig.DBInstance);
    string memberId = "";
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";

        if (context.Request.Cookies["memberInfo"] != null && Common.CheckUser(context.Request.Cookies["memberInfo"].Value))
        {
            memberId = context.Request.Cookies["memberInfo"].Value.Split(new string[] { "$$" }, StringSplitOptions.RemoveEmptyEntries)[0];
            
            string flag = Common.NullToEmpty(context.Request["flag"]);

            string parentid = Common.NullToEmpty(context.Request["parentid"]);
            string city = Common.NullToEmpty(context.Request["city"]);

            switch (flag.ToLower())
            {
                case "list":
                    context.Response.Write(GetCityList(parentid));
                    break;
                case "citystr":
                    context.Response.Write(GetCityStr(city));
                    break;
            }
        }
        else
        {
            context.Response.Write("please login");
        }
    }

    private string GetCityList(string parentid)
    {
        string json = "";

        string strSql = @"select area_id as id,area_name as city from T_Base_Area where area_pid=@parentid";
        SqlParameter[] p = new SqlParameter[] {
            new SqlParameter("@parentid",parentid)
        };
        json += JSONHelper.GetJSON(ado.ExecuteSqlDataset(strSql, p).Tables[0]);
        return json;
    }

    private string GetCityStr(string city)
    {
        string strSql = "select area_id as id,area_name as city from T_Base_Area where area_name like '%'+@area_name+'%' order by area_level desc";
        SqlParameter[] p = new SqlParameter[] { 
                    new SqlParameter("@area_name",city)
                };
        return JSONHelper.GetJSON(ado.ExecuteSqlDataset(strSql, p).Tables[0]);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}